package io.github.loan.system.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import io.github.loan.common.util.PageInfoUtil;
import io.github.loan.system.dao.ICateDao;
import io.github.loan.system.dao.IRegionDao;
import io.github.loan.system.pojo.Cate;
import io.github.loan.system.pojo.Region;
import io.github.loan.system.service.ISystemService;

/**
 * 
 * @author Andone
 *
 */
@Service("systemService")
public class SystemServiceImpl implements ISystemService{

	@Resource
	private IRegionDao regionDao;
	@Resource
	private ICateDao cateDao;
	
	@Override
	public List<Region> findRegionCondListService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		List<Region> regionList=Collections.EMPTY_LIST;
		if(condMap.get("keyword")!=null){
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if(pageInfoUtil!=null){
			condMap.put("pageCount","true");
			condMap.put("currRecord",pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize",pageInfoUtil.getPageSize());
			/*查询总记录数*/
			regionList=this.regionDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(regionList.get(0).getId());
			
			/*查询分页内容*/
			condMap.put("pageCount", "false");
			regionList=this.regionDao.findCondList(condMap);
		}else{
			//condMap.put("pageCount","false");
			regionList=this.regionDao.findCondList(condMap);
		}
		return regionList;
	}

	@Override
	public Region findOneRegionService(Map<String, Object> condMap) {
		return this.regionDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneRegionService(Region region) {
		JSONObject resultJSON=new JSONObject();
		
		int res=this.regionDao.insert(region);
		/*String str=null;
		str.toString();*/
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
			/*放置二级JSON,影响类型*/
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", region.getId());
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneRegionService(Region region) {
		JSONObject resultJSON=new JSONObject();
		int res=this.regionDao.update(region);
		/*String str=null;
		str.toString();*/
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			/*放置二级JSON,影响类型*/
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", region.getId());
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneRegionService(Region region) {
		JSONObject resultJSON=new JSONObject();
		int res=this.regionDao.delete(region);
		/*String str=null;
		str.toString();*/
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
			/*放置二级JSON,影响类型*/
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", region.getId());
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON;
	}
	/*************分类操作开始*************/
	@Override
	public List<Cate> findCateCondListService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		List<Cate> cateList=Collections.EMPTY_LIST;
		if(condMap.get("keyword")!=null){
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if(pageInfoUtil!=null){
			condMap.put("pageCount","true");
			condMap.put("currRecord",pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize",pageInfoUtil.getPageSize());
			/*查询总记录数*/
			cateList=this.cateDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(cateList.get(0).getId());
			
			/*查询分页内容*/
			condMap.put("pageCount", "false");
			cateList=this.cateDao.findCondList(condMap);
		}else{
			//condMap.put("pageCount","false");
			cateList=this.cateDao.findCondList(condMap);
		}
		return cateList;
	}

	@Override
	public Cate findOneCateService(Map<String, Object> condMap) {
		return this.cateDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneCateService(Cate cate) {
JSONObject resultJSON=new JSONObject();
		
		int res=this.cateDao.insert(cate);
		/*String str=null;
		str.toString();*/
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
			/*放置二级JSON,影响类型*/
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", cate.getId());
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneCateService(Cate cate) {
		JSONObject resultJSON=new JSONObject();
		int res=this.cateDao.update(cate);
		/*String str=null;
		str.toString();*/
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			/*放置二级JSON,影响类型*/
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", cate.getId());
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneCateService(Cate cate) {
		JSONObject resultJSON=new JSONObject();
		int res=this.cateDao.delete(cate);
		/*String str=null;
		str.toString();*/
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
			/*放置二级JSON,影响类型*/
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", cate.getId());
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON;
	}
	
}
